package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.repository.CustomerStatementReportReadRepository;
import com.alibaba.citrus.ots.common.util.DateUtils;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsapplicationreportsdk.customerstatementreport.model.CustomerStatementReport;
import com.epoch.app.otsapplicationreportsdk.customerstatementreport.service.CustomerStatementReportService;
import com.epoch.app.otsapplicationreportsdk.model.dto.CustomerStatementReportLoadListVO;
import com.epoch.app.otsapplicationreportsdk.model.dto.CustomerStatementReportSDO;
import com.epoch.app.otsapplicationreportsdk.model.dto.QueryCustomerStatementReportRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2023/10/9 15:33
 */
@Component
public class CustomerStatementReportReadRepositoryImpl implements CustomerStatementReportReadRepository {

    //Log log = Log.getLogger(CustomerStatementReportReadRepositoryImpl.class);

    @Resource
    CustomerStatementReportService customerStatementReportService;

    @Override
    public PageResult<List<CustomerStatementReportSDO>> pageQueryList(QueryCustomerStatementReportRequest request) {
        CustomerStatementReportLoadListVO loadListVO = CustomerStatementReportLoadListVO.newCustomerStatementReportLoadListVOBuilder()
                .customerCodes(request.getCustomerCodes())
                .yearMonth(DateUtils.toDateStr(request.getQueryDate(),DateUtils.DATE_FORMAT))
                .build();
        loadListVO.setStart(request.getStart());
        loadListVO.setLimit(request.getLimit());
        Result<List<CustomerStatementReport>> response = customerStatementReportService.loadListForPage(loadListVO);
        List<CustomerStatementReportSDO> result = response.getResult().stream().map(x -> {
            CustomerStatementReportSDO bean = new CustomerStatementReportSDO();
            BeanUtils.copyProperties(x,bean);
            if (StringUtils.equalsAny(bean.getDocName(),"期初","总计")) {
                bean.setInvoiceDate("");
            }else {
                try {
                    bean.setInvoiceDate(DateUtils.toDateStr(DateUtils.formatString2Date(bean.getInvoiceDate(),DateUtils.PATTEN_YMD_PLAIN),DateUtils.DATE_FORMAT));
                } catch (Exception e) {
                    bean.setInvoiceDate("");
                }
            }
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,response.getTotal());
    }
}
